package com.bpmn.system.controller;

import cn.dev33.satoken.apikey.SaApiKeyManager;
import cn.dev33.satoken.apikey.template.SaApiKeyUtil;
import com.bpmn.common.annotation.ResponseInfoWrapper;
import com.bpmn.common.utils.BeanUtil;
import com.bpmn.system.domain.SysApiKeyDataModel;
import com.bpmn.system.domain.form.ApiKeyDataModelForm;
import com.bpmn.system.domain.query.ApiKeyDataQuery;
import com.bpmn.system.domain.vo.ApiKeyDataVo;
import com.bpmn.system.service.IApiKeyDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * API Key 数据管理接口
 *
 * @author bpmn
 */
@RestController
@RequiredArgsConstructor
@ResponseInfoWrapper
@RequestMapping("/apiKeyData")
@Tag(name = "API Key 数据管理接口", description = "用于管理用户 API Key 数据的接口")
public class ApiKeyDataController {

    private final IApiKeyDataService apiKeyDataService;

    @GetMapping("/list")
    @Operation(summary = "获取API Key数据列表", description = "返回符合条件的API Key数据列表")
    public List<ApiKeyDataVo> list(ApiKeyDataQuery apiKeyDataQuery) {
        List<SysApiKeyDataModel> apiKeyDataModels = apiKeyDataService.selectApiKeyDataList(apiKeyDataQuery);
        List<ApiKeyDataVo> apiKeyDataVos = new ArrayList<>();
        for (SysApiKeyDataModel apiKeyDataModel : apiKeyDataModels) {
            ApiKeyDataVo vo = BeanUtil.toBean(apiKeyDataModel, ApiKeyDataVo.class);
            if (vo.getCreateTime() != null) {
                vo.setCreateTime(new Date(apiKeyDataModel.getCreateTime()));
            }
            if (vo.getExpiresTime() != null) {
                vo.setExpiresTime(new Date(apiKeyDataModel.getExpiresTime()));
            }
            apiKeyDataVos.add(vo);
        }
        return apiKeyDataVos;
    }

    @PostMapping("/create")
    @Operation(summary = "创建新的API Key数据", description = "创建一个新的API Key数据记录")
    public SysApiKeyDataModel create(@RequestBody ApiKeyDataModelForm sysApiKeyDataModel) {
        String apiKeyValue = SaApiKeyManager.getSaApiKeyTemplate().randomApiKeyValue();
        SysApiKeyDataModel apiKeyDataModel = BeanUtil.toBean(sysApiKeyDataModel, SysApiKeyDataModel.class);
        apiKeyDataModel.setApiKey(apiKeyValue);
        apiKeyDataModel.setExpiresTime(sysApiKeyDataModel.getExpiresTime().getTime());
        // 设置创建时间
        apiKeyDataModel.setCreateTime(System.currentTimeMillis());
        apiKeyDataService.save(apiKeyDataModel);
        return apiKeyDataModel;
    }

    @PutMapping("/update")
    @Operation(summary = "更新API Key数据", description = "修改已有API Key数据的信息")
    public Boolean update(@RequestBody ApiKeyDataModelForm sysApiKeyDataModel) {
        SysApiKeyDataModel apiKeyDataModel = BeanUtil.toBean(sysApiKeyDataModel, SysApiKeyDataModel.class);
        apiKeyDataModel.setExpiresTime(sysApiKeyDataModel.getExpiresTime().getTime());
        apiKeyDataService.updateById(apiKeyDataModel);
        return true;
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除API Key数据", description = "根据API Key删除对应的数据")
    @Parameter(name = "apiKey", description = "要删除的API Key字符串")
    public Boolean delete(@PathVariable String id) {
        SysApiKeyDataModel sysApiKeyDataModel = apiKeyDataService.getById(id);
        if (sysApiKeyDataModel == null) {
            return false;
        }
        apiKeyDataService.removeById(id);
        SaApiKeyUtil.deleteApiKey(sysApiKeyDataModel.getApiKey());
        return true;
    }

}